package com.ql.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ql.dao.SectionDao;
import com.ql.domain.response.JsonResult;
import com.ql.domain.Section;
import com.ql.service.SectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SectionServiceImpl implements SectionService {
    @Autowired
    SectionDao sectionDao;


    @Override

    public JsonResult findAll(String sectionName, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Section> all = sectionDao.findAll(sectionName);
        PageInfo pageInfo = new PageInfo(all);
        return JsonResult.success(pageInfo.getList(), pageInfo.getTotal());
    }

    @Override
    public JsonResult save(Section section) {
        try {
            sectionDao.save(section);
            return JsonResult.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("添加失败");
        }
    }

    @Override
    public JsonResult update(Section section) {
        try {
            sectionDao.update(section);
            return JsonResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @Override
    public List<Section> sectionGroupName(String userId) {
        return sectionDao.getSectionGroupName(userId);
    }

    @Override
    public List<Section> sectionGroupNameByProjectId(String projectId,String userId) {
        return sectionDao.sectionGroupNameByProjectId(projectId,userId);
    }

    @Override
    public JsonResult del(String sectionId) {
        try {
            //首先删除中间表
//            sectionDao.delUserSectionBySectionId( sectionId);
            sectionDao.del(sectionId);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

}
